<!DOCTYPE html>






  


<html class="theme-next pisces use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2"/>
<meta name="theme-color" content="#222">












<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />






















<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=6.2.0" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.2.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=6.2.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=6.2.0">


  <link rel="mask-icon" href="/images/logo.svg?v=6.2.0" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '6.2.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="一、继承关系java.lang.Object     ↳    android.content.Context        ↳    android.content.ContextWrapper             ↳    android.view.ContextThemeWrapper                 ↳  android.app.Activity">
<meta name="keywords" content="Activity">
<meta property="og:type" content="article">
<meta property="og:title" content="Activity学习之PreferenceActivity">
<meta property="og:url" content="http://blog.bisondev.cn/2017/04/30/Activity学习之PreferenceActivity/index.html">
<meta property="og:site_name" content="Bison分享日志">
<meta property="og:description" content="一、继承关系java.lang.Object     ↳    android.content.Context        ↳    android.content.ContextWrapper             ↳    android.view.ContextThemeWrapper                 ↳  android.app.Activity">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2017-04-30T08:16:32.263Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Activity学习之PreferenceActivity">
<meta name="twitter:description" content="一、继承关系java.lang.Object     ↳    android.content.Context        ↳    android.content.ContextWrapper             ↳    android.view.ContextThemeWrapper                 ↳  android.app.Activity">






  <link rel="canonical" href="http://blog.bisondev.cn/2017/04/30/Activity学习之PreferenceActivity/"/>



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>Activity学习之PreferenceActivity | Bison分享日志</title>
  









  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Bison分享日志</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">I'm not perfect,but I keep trying</p>
      
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>




<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">
    <a href="/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-about">
    <a href="/about/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">
    <a href="/tags/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />标签</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">
    <a href="/categories/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-th"></i> <br />分类</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">
    <a href="/archives/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-schedule">
    <a href="/schedule/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-calendar"></i> <br />日程表</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-sitemap">
    <a href="/sitemap.xml" rel="section">
      <i class="menu-item-icon fa fa-fw fa-sitemap"></i> <br />站点地图</a>
  </li>

      
      
    </ul>
  

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.bisondev.cn/2017/04/30/Activity学习之PreferenceActivity/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Bison Qin">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Bison分享日志">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Activity学习之PreferenceActivity
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2017-04-30 15:37:15 / 修改时间：16:16:32" itemprop="dateCreated datePublished" datetime="2017-04-30T15:37:15+08:00">2017-04-30</time>
            

            
              

              
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Android/" itemprop="url" rel="index"><span itemprop="name">Android</span></a></span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2017/04/30/Activity学习之PreferenceActivity/" class="leancloud_visitors" data-flag-title="Activity学习之PreferenceActivity">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数：</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="一、继承关系"><a href="#一、继承关系" class="headerlink" title="一、继承关系"></a>一、继承关系</h2><pre><code>java.lang.Object  
  ↳    android.content.Context  
     ↳    android.content.ContextWrapper  
          ↳    android.view.ContextThemeWrapper  
              ↳  android.app.Activity  
                 ↳ android.app.ListActivity  
                    ↳ android.preference.PreferenceActivity  
</code></pre><h2 id="二、简介"><a href="#二、简介" class="headerlink" title="二、简介"></a>二、简介</h2><p>当开发一个Android应用程序时，不可避免地需要进行选项设置，这些选项设置会以参数的形式保存，习惯上我们会用Preferences进行保存。Android3.0开始，Android推荐PreferenceActivity与PreferenceFragment结合使用，其中PreferenceActivity只负责加载选项设置列表的布局文件，PreferenceFragment才负责加载选项设置的布局文件</p>
<h2 id="三、选项布局子属性"><a href="#三、选项布局子属性" class="headerlink" title="三、选项布局子属性"></a>三、选项布局子属性</h2><ol>
<li>Preference（仅供显示的参数）  </li>
<li>PreferenceScreen（根元素）  </li>
<li><p>PreferenceCategory（参数组）<br> a. 主要属性<br> ○ tittle：参数组的标题</p>
</li>
<li><p>CheckBoxPreference（复选框参数）<br> a. 主要的属性<br> ○ key：保存在Preferences中的键值<br> ○ tittle：该选项设置的标题<br> ○ summaryOn：当选中复选框时显示的提示短文<br> ○ summaryOff：当复选框没被选中时显示的提示短文<br> ○ defaultValue：复选框的默认状态  </p>
</li>
<li><p>EditTextPreference（文本框输入参数）<br> a. 主要属性<br> ○ key：保存在Preferences中的键值<br> ○ tittle：该选项设置的标题<br> ○ summary：提示的短文<br> ○ dialogTittle：弹出的对话框的标题<br> ○ dialogMessage：弹出的对话框的信息<br> ○ dialogIcon：弹出对话框的图标<br> ○ dialogLayout：弹出对话框的布局  </p>
</li>
</ol>
<a id="more"></a>        
<ol start="6">
<li><p>ListPreference（列表框输入参数）<br> a. 主要属性<br> ○ key：保存在Preferences中的键值<br> ○ tittle：该选项设置的标题<br> ○ summary：提示的短文<br> ○ dialogTittle：弹出的对话框的标题<br> ○ entries：会展示给用户看的选项名字<br> ○ entriesValues：会存在Preference中的值  </p>
</li>
<li><p>MultiSelectListPreference（多选列表输入参数）<br> a. 主要属性<br> ○ key：保存在Preferences中的键值<br> ○ tittle：该选项设置的标题<br> ○ summary：提示的短文<br> ○ dialogTittle：弹出的对话框的标题<br> ○ entries：会展示给用户看的选项名字<br> ○ entriesValues：会存在Preference中的值  </p>
</li>
<li><p>RingtonePreference（系统铃声选择）<br> a. 主要属性<br> ○ key：存储在Preference中的键名<br> ○ tittle：该设置选项的标题<br> ○ summary：提示的短文<br> ○ showDefault：是否显示默认选中的项<br> ○ showSilent：是否显示静音的选项  </p>
</li>
<li><p>SwitchPreference（开关输入参数）<br> a. 主要属性<br> ○ key：存储在Preference中的键名<br> ○ tittle：该设置选项的标题<br> ○ summaryOn：当选中复选框时显示的提示短文<br> ○ summaryOff：当复选框没被选中时显示的提示短文<br> ○ defaultValue：默认的值  </p>
</li>
</ol>
<h2 id="四、用法"><a href="#四、用法" class="headerlink" title="四、用法"></a>四、用法</h2><ol>
<li><p>继承PreferenceActivity<br>PreferenceActivityTest的代码  </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PreferenceActivityTest</span> <span class="keyword">extends</span> <span class="title">PreferenceActivity</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(Bundle savedInstanceState)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.onCreate(savedInstanceState);</span><br><span class="line">        <span class="comment">//该方法用于为该界面设置一个标题按钮</span></span><br><span class="line">        <span class="keyword">if</span>(hasHeaders()) &#123;</span><br><span class="line">            Button button = <span class="keyword">new</span> Button(<span class="keyword">this</span>);</span><br><span class="line">            button.setText(<span class="string">"设置操作"</span>);</span><br><span class="line">            <span class="comment">//将该按钮添加到该界面上</span></span><br><span class="line">            setListFooter(button);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 重写该方法，负责加载界面布局文件</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> target</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onBuildHeaders</span><span class="params">(List&lt;Header&gt; target)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//加载选项设置列表的布局文件</span></span><br><span class="line">        loadHeadersFromResource(R.xml.preference_headers, target);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 重写该方法，验证个PreferenceFragment是否有效</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> fragmentName</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">boolean</span> <span class="title">isValidFragment</span><span class="params">(String fragmentName)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Fragment1</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">Prefs1Fragment</span> <span class="keyword">extends</span> <span class="title">PreferenceFragment</span> </span>&#123;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(@Nullable Bundle savedInstanceState)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">super</span>.onCreate(savedInstanceState);</span><br><span class="line">            addPreferencesFromResource(R.xml.preferences);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Fragment2</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">Prefs2Fragment</span> <span class="keyword">extends</span> <span class="title">PreferenceFragment</span> </span>&#123;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(@Nullable Bundle savedInstanceState)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">super</span>.onCreate(savedInstanceState);</span><br><span class="line">            addPreferencesFromResource(R.xml.display_prefs);</span><br><span class="line">            <span class="comment">//获取传入该Fragment的参数</span></span><br><span class="line">            String website = getArguments().getString(<span class="string">"website"</span>);</span><br><span class="line">            Toast.makeText(getActivity(), <span class="string">"拿到的数据是："</span> + website, Toast.LENGTH_LONG).show();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>编写选项设置列表布局文件（根节点preference-headers）<br>preference_headers.xml的代码    </p>
 <figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">	</span><br><span class="line"><span class="tag">&lt;<span class="name">preference-headers</span> <span class="attr">xmlns:android</span>=<span class="string">"http://schemas.android.com/apk/res/android"</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">    <span class="comment">&lt;!-- 指定启动指定PreferenceFragment的列表项 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">header</span> <span class="attr">android:fragment</span>=<span class="string">"cn.bisondev.learnandroid.learnactivity.PreferenceActivityTest$Prefs1Fragment"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:icon</span>=<span class="string">"@mipmap/ic_launcher"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:title</span>=<span class="string">"程序选项设置"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:summary</span>=<span class="string">"设置应用的相关选项"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 指定启动指定PreferenceFragment的列表项 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">header</span> <span class="attr">android:fragment</span>=<span class="string">"cn.bisondev.learnandroid.learnactivity.PreferenceActivityTest$Prefs2Fragment"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:icon</span>=<span class="string">"@mipmap/ic_launcher_round"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:title</span>=<span class="string">"界面选项设置"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:summary</span>=<span class="string">"设置显示界面的相关选项"</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- 使用extra可向Activity传入额外的数据 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">extra</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:name</span>=<span class="string">"website"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:value</span>=<span class="string">"www.bisondev.cn"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">header</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 使用Intent启动指定Activity的列表项 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">header</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:icon</span>=<span class="string">"@mipmap/ic_launcher"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:title</span>=<span class="string">"使用Intent"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:summary</span>=<span class="string">"使用Intent启动某个Activity"</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="tag">&lt;<span class="name">intent</span> <span class="attr">android:action</span>=<span class="string">"android.intent.action.VIEW"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:data</span>=<span class="string">"http://www.bisondev.cn"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">header</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">preference-headers</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
</ol>
<ol start="3">
<li><p>编写选项设置的布局文件（根节点PreferenceScreen ）<br>preferences.xml的布局文件</p>
 <figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line">	</span><br><span class="line">&lt;PreferenceScreen xmlns:android=<span class="string">"http://schemas.android.com/apk/res/android"</span>&gt;</span><br><span class="line">    &lt;!-- 设置系统铃声 --&gt;</span><br><span class="line">    &lt;RingtonePreference</span><br><span class="line">        android:ringtoneType=<span class="string">"all"</span></span><br><span class="line">        android:title=<span class="string">"设置铃声"</span></span><br><span class="line">        android:summary=<span class="string">"选择铃声（测试RingPreference）"</span></span><br><span class="line">        android:showDefault=<span class="string">"true"</span></span><br><span class="line">        android:key=<span class="string">"ring_key"</span></span><br><span class="line">        android:showSilent=<span class="string">"true"</span>&gt;</span><br><span class="line">    &lt;/RingtonePreference&gt;</span><br><span class="line"></span><br><span class="line">    &lt;PreferenceCategory android:title=<span class="string">"个人信息设置组"</span>&gt;</span><br><span class="line">        &lt;!-- 通过输入框填写用户名 --&gt;</span><br><span class="line">        &lt;EditTextPreference</span><br><span class="line">            android:key=<span class="string">"name"</span></span><br><span class="line">            android:title=<span class="string">"填写用户名"</span></span><br><span class="line">            android:summary=<span class="string">"填写您的用户名（测试EditPreference）"</span></span><br><span class="line">            android:dialogIcon=<span class="string">"@mipmap/ic_launcher"</span></span><br><span class="line">            android:dialogTitle=<span class="string">"您所使用的用户名为："</span></span><br><span class="line">            android:dialogMessage=<span class="string">"测试一下dialogMessage"</span> /&gt;</span><br><span class="line"></span><br><span class="line">        &lt;!-- 通过列表框选择性别 --&gt;</span><br><span class="line">        &lt;ListPreference</span><br><span class="line">            android:key=<span class="string">"gender"</span></span><br><span class="line">            android:title=<span class="string">"性别"</span></span><br><span class="line">            android:summary=<span class="string">"选择您的性别（测试ListPreference）"</span></span><br><span class="line">            android:dialogTitle=<span class="string">"ListPreference"</span></span><br><span class="line">            android:entries=<span class="string">"@array/gender_name_list"</span></span><br><span class="line">            android:entryValues=<span class="string">"@array/gender_value_list"</span>/&gt;</span><br><span class="line"></span><br><span class="line">        &lt;MultiSelectListPreference</span><br><span class="line">            android:key=<span class="string">"tag"</span></span><br><span class="line">            android:title=<span class="string">"标签"</span></span><br><span class="line">            android:summary=<span class="string">"选择你的标签"</span></span><br><span class="line">            android:entries=<span class="string">"@array/tag_list"</span></span><br><span class="line">            android:entryValues=<span class="string">"@array/tag_value_list"</span></span><br><span class="line">            android:dialogTitle=<span class="string">"性格"</span>/&gt;</span><br><span class="line">    &lt;/PreferenceCategory&gt;</span><br><span class="line"></span><br><span class="line">    &lt;PreferenceCategory android:title=<span class="string">"系统功能设置组"</span>&gt;</span><br><span class="line">        &lt;CheckBoxPreference</span><br><span class="line">            android:key=<span class="string">"autoSave"</span></span><br><span class="line">            android:title=<span class="string">"自动保存进度"</span></span><br><span class="line">            android:summaryOn=<span class="string">"自动保存：开启"</span></span><br><span class="line">            android:summaryOff=<span class="string">"自动保存：关闭"</span></span><br><span class="line">            android:defaultValue=<span class="string">"true"</span>/&gt;</span><br><span class="line">    &lt;/PreferenceCategory&gt;</span><br><span class="line">&lt;/PreferenceScreen&gt;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<pre><code>display_prefs.xml的布局代码

    <figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">	<span class="tag">&lt;<span class="name">PreferenceScreen</span> <span class="attr">xmlns:android</span>=<span class="string">"http://schemas.android.com/apk/res/android"</span>&gt;</span></span><br><span class="line">	    <span class="tag">&lt;<span class="name">PreferenceCategory</span> <span class="attr">android:title</span>=<span class="string">"背景灯光组"</span>&gt;</span></span><br><span class="line">	       <span class="comment">&lt;!-- 通过列表框选择灯光强度 --&gt;</span></span><br><span class="line">	       <span class="tag">&lt;<span class="name">ListPreference</span></span></span><br><span class="line"><span class="tag">	           <span class="attr">android:key</span>=<span class="string">"light"</span></span></span><br><span class="line"><span class="tag">	           <span class="attr">android:title</span>=<span class="string">"灯光强度"</span></span></span><br><span class="line"><span class="tag">	           <span class="attr">android:summary</span>=<span class="string">"请选择灯光强度（测试ListPreference）"</span></span></span><br><span class="line"><span class="tag">	           <span class="attr">android:dialogTitle</span>=<span class="string">"请选择灯光强度"</span></span></span><br><span class="line"><span class="tag">	           <span class="attr">android:entries</span>=<span class="string">"@array/light_strength_list"</span></span></span><br><span class="line"><span class="tag">	           <span class="attr">android:entryValues</span>=<span class="string">"@array/light_strength_list"</span>/&gt;</span></span><br><span class="line">	   <span class="tag">&lt;/<span class="name">PreferenceCategory</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	   <span class="tag">&lt;<span class="name">PreferenceCategory</span> <span class="attr">android:title</span>=<span class="string">"文字显示组"</span>&gt;</span></span><br><span class="line">	       <span class="comment">&lt;!-- 通过SwitchPreference设置是否自动滚屏 --&gt;</span></span><br><span class="line">	       <span class="tag">&lt;<span class="name">SwitchPreference</span></span></span><br><span class="line"><span class="tag">	           <span class="attr">android:key</span>=<span class="string">"autoScroll"</span></span></span><br><span class="line"><span class="tag">	           <span class="attr">android:title</span>=<span class="string">"自动滚屏"</span></span></span><br><span class="line"><span class="tag">	           <span class="attr">android:summaryOn</span>=<span class="string">"自动滚屏：开"</span></span></span><br><span class="line"><span class="tag">	           <span class="attr">android:summaryOff</span>=<span class="string">"自动滚屏：关"</span></span></span><br><span class="line"><span class="tag">	           <span class="attr">android:defaultValue</span>=<span class="string">"true"</span>/&gt;</span></span><br><span class="line">	   <span class="tag">&lt;/<span class="name">PreferenceCategory</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">PreferenceScreen</span>&gt;</span></span><br></pre></td></tr></table></figure>
</code></pre><ol start="4">
<li><p>编写设置参数值数组<br>尽量用string-array，要不然用array，当值为数字时，很容易报空指针异常</p>
 <figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">		</span><br><span class="line"><span class="tag">&lt;<span class="name">string-array</span> <span class="attr">name</span>=<span class="string">"gender_name_list"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span>&gt;</span>男<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span>&gt;</span>女<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">string-array</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">string-array</span> <span class="attr">name</span>=<span class="string">"gender_value_list"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span>&gt;</span>0<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span>&gt;</span>1<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">string-array</span>&gt;</span></span><br><span class="line">	</span><br><span class="line"><span class="tag">&lt;<span class="name">string-array</span> <span class="attr">name</span>=<span class="string">"light_strength_list"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span>&gt;</span>1<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span>&gt;</span>2<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span>&gt;</span>3<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span>&gt;</span>4<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">string-array</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">string-array</span> <span class="attr">name</span>=<span class="string">"light_value_list"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span>&gt;</span>1<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span>&gt;</span>2<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span>&gt;</span>3<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span>&gt;</span>4<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">string-array</span>&gt;</span></span><br><span class="line">	</span><br><span class="line"><span class="tag">&lt;<span class="name">string-array</span> <span class="attr">name</span>=<span class="string">"tag_list"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span>&gt;</span>活泼<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span>&gt;</span>开朗<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span>&gt;</span>外向<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span>&gt;</span>沉稳<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">string-array</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">string-array</span> <span class="attr">name</span>=<span class="string">"tag_value_list"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span>&gt;</span>0<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span>&gt;</span>1<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span>&gt;</span>2<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span>&gt;</span>3<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">string-array</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
</ol>

      
    </div>

    

    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Activity/" rel="tag"># Activity</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/04/29/Ubuntu装机那些事儿/" rel="next" title="Ubuntu装机那些事儿">
                <i class="fa fa-chevron-left"></i> Ubuntu装机那些事儿
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/04/30/Activity学习之ExpandableListActivity/" rel="prev" title="Activity学习之ExpandableListActivity">
                Activity学习之ExpandableListActivity <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">Bison Qin</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">13</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">4</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">8</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          

          
          

          
          

          
            
          
          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#一、继承关系"><span class="nav-number">1.</span> <span class="nav-text">一、继承关系</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#二、简介"><span class="nav-number">2.</span> <span class="nav-text">二、简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#三、选项布局子属性"><span class="nav-number">3.</span> <span class="nav-text">三、选项布局子属性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#四、用法"><span class="nav-number">4.</span> <span class="nav-text">四、用法</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Bison Qin</span>

  

  
</div>




  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动 v3.7.1</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/theme-next/hexo-theme-next">NexT.Pisces</a> v6.2.0</div>




        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.2.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.2.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=6.2.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=6.2.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.2.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.2.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.2.0"></script>



  



	





  





  










  





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("M90nbI9KgVkqdeNl4kIE1aY4-gzGzoHsz", "3I8HRy0nT1nMlMw7enfvrnu5");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            
              var $element = $(document.getElementById(url));
              $element.find('.leancloud-visitors-count').text(counter.get('time'));
            
            counter.save(null, {
              success: function(counter) {
                
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            
              var $element = $(document.getElementById(url));
              $element.find('.leancloud-visitors-count').text('Counter not initialized! See more at console err msg.');
              console.error('ATTENTION! LeanCloud counter has security bug, see here how to solve it: https://github.com/theme-next/hexo-leancloud-counter-security. \n But you also can use LeanCloud without security, by set \'security\' option to \'false\'.');
            
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  

  
  

  

  

  

  

  

</body>
</html>
